<?php

namespace App\Http\Controllers\Admin\Orders;

use App\Http\Controllers\Controller as BaseController;
use App\Models\Orders\OrdersUserModel;
use App\Repository\Orders\HandlesRepository;
use Illuminate\Http\Request;
use App\Repository\GoodsPhone\GoodsPhoneRepository;
use Illuminate\Support\Facades\DB;

// 专门处理订单修改
class HandlesController extends BaseController
{
    private $repoist;

    public function __construct(HandlesRepository $repository)
    {
        $this->repoist = $repository;
    }

//    选号下单的选号接口,调用Api里的数据
    public function getSelectNumber(Request $request)
    {
        if (!$request->get('goodsId')) {
            return $this->returnMsg(1, '请传递商品id参数');
        }
        return $this->repoist->getSelectNumber($request->all());

    }
    public function getSelectNumberTwo(Request $request)
    {
        if (!$request->get('goodsId')) {
            return $this->returnMsg(1, '请传递商品id参数');
        }
        $models = new GoodsPhoneRepository();
        $where['where'] = ['goods_id'=>$request->get('goodsId')];
        if(!empty($request->get('symbol'))){
            $where['like'] = ['type'=>$request->get('symbol')];
        }
        if(!empty($request->get('city'))){
            $city = $request->get('city');
            if (mb_strlen($city) > 2) {
                $city = mb_substr($city, 0, 2);
            }
            $where['like'] = ['city'=>$city];
        }
        if(!empty($request->get('phone'))){
            $phones = $request->get('phone');
            // var_dump($phones['two'],111111111);

            if(!empty($phones)){
                $str = '';
                if(!empty($phones['two'])){
                     $str .= "SUBSTRING(`phone`,2,1)=".$phones['two'];
                }
                if(!empty($phones['three'])){
                    if(!empty($str)){
                        $str .= '  and  ';
                    }
                    $str .= "SUBSTRING(`phone`,3,1)=" .$phones['three'];
                }
                if(!empty($phones['four'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,4,1)=".$phones['four'];
                }
                if(!empty($phones['five'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,5,1)=".$phones['five'];
                }
                if(!empty($phones['six'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,6,1)=".$phones['six'];
                }
                if(!empty($phones['server'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,7,1)=".$phones['server'];
                }
                if(!empty($phones['eight'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,8,1)=".$phones['eight'];
                }
                if(!empty($phones['nine'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,9,1)=". $phones['nine'];
                }
                if(!empty($phones['ten'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .="SUBSTRING(`phone`,10,1)=". $phones['ten'];
                }
                if(!empty($phones['eleven'])){
                    if(!empty($str)){
                        $str .= ' and ';
                    }
                    $str .= "SUBSTRING(`phone`,11,1)=".$phones['eleven'];
                }
                $where['raw'] = $str;
            }
        }
        
        
        $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('amounts');
        $where['limit']['number'] = $request->get('amounts');
        // var_dump($where);
        return $models->index($where);

    }

// 拉黑订单
    public function changeBlocking(Request $request)
    {
        $params = $request->all();
        return $this->repoist->blocking($params);
    }

//    修改商品
    public function changeGoodsInfo(Request $request)
    {
        $params = $request->all();
        return $this->repoist->changeGoodsInfo($params);
    }

//    修改信息
    public function changeInfo(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
//        return $this->returnMsg(1, '请传递id参数');
        $params = $request->all();
        return $this->repoist->changeInfo($params);
    }


    // 修改备注
    public function changeRemark(Request $request)
    {
        $params = $request->all();
        return $this->repoist->changeRemark($params);
    }

//    修改字单状态
    public function changeStatus(Request $request)
    {
        $params = $request->all();
        //备注信息添加 和操作
        return $this->repoist->changeStatus($params);
    }

    public function ip138SkumarkAreaApiBackSave(Request $request)
    {
        return $this->repoist->editSkuArea($request->all());
    }

    public function getSkuArea(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $ModelOrdersUser = new OrdersUserModel();
        $id = $request->get('id');
        $value = $ModelOrdersUser->where(['id' => $id])->value('sku_area');
        return $this->returnMsg(200, $value, ['sku_area' => $value]);
    }

}
